package controller

import (
	"awesomeProject/lib"
	"github.com/gin-gonic/gin"
	"log"
)

func DeleteFriend(c *gin.Context) {
	var req lib.DeleteFrdReq
	if err := c.ShouldBindJSON(&req); err != nil {
		log.Println("[delete friend] read json fail")
		return
	}
	ok, err := AuthId(req.Id, req.Token)
	if !ok || err != nil {
		log.Println("[delete friend] auth fail")
		return
	}
	id := req.Id
	friendId := req.TheOtherId
	stmt, err := pool.Prepare("delete from public.relation where(id_1=$1 and id_2=$2) or (id_1=$3 and id_2=$4)")
	if err != nil {
		log.Println("[delete friend] create sql statement fail")
		return
	}
	_, err = stmt.Exec(id, friendId, id, friendId)
	_, err = stmt.Exec(id, friendId, id, friendId)
	if err != nil {
		log.Println("[delete friend] delete relation from database fail")
		c.JSON(400, lib.DeleteFrdResp{
			StatusCode: 1,
			StatusMsg:  "something wrong in database, maybe you aren't friends yet",
		})
		return
	}
	c.JSON(200, lib.DeleteFrdResp{
		StatusMsg:  "delete friend successfully",
		StatusCode: 2,
	})
	return
}
